#include "head.h"    

int init_config(struct Config *config) {
    config->hostName[0] = '\0';
    config->ipAddress[0] = '\0';
    config->masterIp[0] = '\0';
    config->masterPort = 0;
    config->interTime.cpu.time = 5;
    config->interTime.cpu.type = 1;
    config->interTime.mem.time = 10;
    config->interTime.mem.type = 2;
    config->interTime.disk.time = 120;
    config->interTime.disk.time = 3;
    config->interTime.proc.time = 10;
    config->interTime.proc.time = 4;
    config->interTime.sys.time = 60;
    config->interTime.sys.type = 5;
    config->interTime.user.type = 600;
    config->interTime.user.type = 6;
    config->nworker = 3;
}

int get_config(const char *file, struct Config *config) {
    FILE *fp = NULL;
    char *buff;
    size_t len = 0;
    if ((fp = fopen(file, "rb")) == NULL) {
        return -1;
    }
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    buff = (char *)calloc(len + 1, sizeof(char));
    if (fread(buff, 1, len, fp) != len) {
        return -1;
    }
    fclose(fp);
    // printf("buff: %s\n", buff);

    const cJSON *master = NULL;
    const cJSON *slave = NULL;
    const cJSON *rabbitmq = NULL;
    cJSON *conf = cJSON_Parse(buff);
    if (conf == NULL) {
        const char *err_ptr = cJSON_GetErrorPtr();
        if (err_ptr != NULL) {
            fprintf(stderr, "Error Before : %s\n", err_ptr);
            cJSON_Delete(conf);
            return -1;
        }
    }
    // DBG("before config get");
    cJSON *hostName = cJSON_GetObjectItemCaseSensitive(conf, "HostName");
    cJSON *ipAddress = cJSON_GetObjectItemCaseSensitive(conf, "IpAddress");
    strcpy(config->hostName, hostName->valuestring);
    strcpy(config->ipAddress, ipAddress->valuestring);
    // DBG("after config get");

    master = cJSON_GetObjectItemCaseSensitive(conf, "Master");
    cJSON *masterIp = cJSON_GetObjectItemCaseSensitive(master, "MasterIp");
    cJSON *masterPort = cJSON_GetObjectItemCaseSensitive(master, "MasterPort");
    strcpy(config->masterIp, masterIp->valuestring);
    config->masterPort = masterPort->valueint;

    slave = cJSON_GetObjectItemCaseSensitive(conf, "Slave");
    cJSON *interval = cJSON_GetObjectItemCaseSensitive(slave, "Interval");
    cJSON *cpu = cJSON_GetObjectItemCaseSensitive(interval, "Cpu");
    cJSON *mem = cJSON_GetObjectItemCaseSensitive(interval, "Mem");
    cJSON *disk = cJSON_GetObjectItemCaseSensitive(interval, "Disk");
    cJSON *proc = cJSON_GetObjectItemCaseSensitive(interval, "Proc");
    cJSON *sys = cJSON_GetObjectItemCaseSensitive(interval, "Sys");
    cJSON *user = cJSON_GetObjectItemCaseSensitive(interval, "User");

    cJSON *worker = cJSON_GetObjectItemCaseSensitive(slave, "Worker");
    config->interTime.cpu.time = cJSON_GetObjectItemCaseSensitive(cpu, "time")->valueint;
    config->interTime.cpu.type = cJSON_GetObjectItemCaseSensitive(cpu, "type")->valueint;
    config->interTime.mem.time = cJSON_GetObjectItemCaseSensitive(mem, "time")->valueint;
    config->interTime.mem.type = cJSON_GetObjectItemCaseSensitive(mem, "type")->valueint;
    config->interTime.disk.time = cJSON_GetObjectItemCaseSensitive(disk, "time")->valueint;
    config->interTime.disk.type = cJSON_GetObjectItemCaseSensitive(disk, "type")->valueint;
    config->interTime.proc.time = cJSON_GetObjectItemCaseSensitive(proc, "time")->valueint;
    config->interTime.proc.type = cJSON_GetObjectItemCaseSensitive(proc, "type")->valueint;
    config->interTime.sys.time = cJSON_GetObjectItemCaseSensitive(sys, "time")->valueint;
    config->interTime.sys.type = cJSON_GetObjectItemCaseSensitive(sys, "type")->valueint;
    config->interTime.user.time = cJSON_GetObjectItemCaseSensitive(user, "time")->valueint;
    config->interTime.user.type = cJSON_GetObjectItemCaseSensitive(user, "type")->valueint;
    config->nworker = worker->valueint;

    rabbitmq = cJSON_GetObjectItemCaseSensitive(conf, "RabbitMQ");
    cJSON *RabbitMQIp = cJSON_GetObjectItemCaseSensitive(rabbitmq, "RabbitMQIp");
    cJSON *RabbitMQPort = cJSON_GetObjectItemCaseSensitive(rabbitmq, "RabbitMQPort");
    strcpy(config->rabbitCon.RabbitMQIp, RabbitMQIp->valuestring);
    config->rabbitCon.RabbitMQPort = RabbitMQPort->valueint;

    return 0;
}

void init_config_server(struct ConfigServer *config) {
    config->myaqlCon.MysqlIp[0] = '\0';
    config->myaqlCon.MysqlPort = 3306;
    config->rabbitCon.RabbitMQIp[0] = '\0';
    config->rabbitCon.RabbitMQPort = 5672;
}

int get_config_server(const char *file, struct ConfigServer *config) {
    FILE *fp = NULL;
    char *buff;
    size_t len = 0;
    if ((fp = fopen(file, "rb")) == NULL) {
        return -1;
    }
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    buff = (char *)calloc(len + 1, sizeof(char));
    if (fread(buff, 1, len, fp) != len) {
        return -1;
    }
    fclose(fp);
    // printf("buff: %s\n", buff);
    DBG(INFO " before cjson");
    cJSON *conf = cJSON_Parse(buff);
    if (conf == NULL) {
        const char *err_ptr = cJSON_GetErrorPtr();
        if (err_ptr != NULL) {
            fprintf(stderr, "Error Before : %s\n", err_ptr);
            cJSON_Delete(conf);
            return -1;
        }
    }
    // DBG("before config get");
    DBG(INFO " before config");
    cJSON *mysqlCon = cJSON_GetObjectItemCaseSensitive(conf, "Mysql");
    cJSON *mysqlIp = cJSON_GetObjectItemCaseSensitive(mysqlCon, "host");
    cJSON *mysqlPort = cJSON_GetObjectItemCaseSensitive(mysqlCon, "port");
    strcpy(config->myaqlCon.MysqlIp, mysqlIp->valuestring);
    config->myaqlCon.MysqlPort = mysqlPort->valueint;

    DBG(INFO " before rabbit config");
    cJSON *rabbitCon = cJSON_GetObjectItemCaseSensitive(conf, "RabbitMQ");
    cJSON *rabbitIp = cJSON_GetObjectItemCaseSensitive(rabbitCon, "RabbitMQIp");
    cJSON *rabbitPort = cJSON_GetObjectItemCaseSensitive(rabbitCon, "RabbitMQPort");
    strcpy(config->rabbitCon.RabbitMQIp, rabbitIp->valuestring);
    config->rabbitCon.RabbitMQPort = rabbitPort->valueint;

    return 0;
}
